import org.junit.Test;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test4 {

}

class Target implements Runnable {
    int target = 100;
    Object obj = new Object();
    MyTarget mt = new MyTarget();
    Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            lock.lock();
            if (target > 0) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "正在售卖" + target-- + "张票");
                lock.unlock();
            } else {
                break;
            }
        }

    }
}

class MyTarget {
    public static void main(String[] args) {
        Target target = new Target();
        Thread thread1 = new Thread(target, "窗口一");
        Thread thread2 = new Thread(target, "窗口二");
        Thread thread3 = new Thread(target, "窗口三");
        thread1.start();
        thread2.start();
        thread3.start();
    }
}
